package com.miyu.test.ehcache;

import lombok.extern.slf4j.Slf4j;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.PersistentCacheManager;
import org.ehcache.Status;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.time.Duration;

/**
 * 动态处理ehcache 缓存工具类
 * ehcache 3.x 缓存工具类
 * 提供 设置 取值  有效期
 * @author longmy
 * @date
 */
public class EhcacheUtils {
    @Autowired
    private static CacheManager cacheManager;

    /**
     * 初始化设置
     *
     */
    private static void initEhcache(String cacheName,String key,String value, long expiry){
        /*CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);*/
        /*初始化内存设置*/
        ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder()
                // 堆内缓存大小
                .heap(100, MemoryUnit.KB)
                // 堆外缓存大小
                .offheap(100, MemoryUnit.MB)
                // 配置磁盘持久化储存(硬盘存储)用来持久化到磁盘,这里设置为false不启用 true 保存到本地
                .disk(1000, MemoryUnit.MB,true);

        CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, resourcePoolsBuilder)
                /*默认过期时间 */
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(50)))
                .withExpiry(ExpiryPolicyBuilder.noExpiration())
                .build();
                // CacheManager管理缓存
        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                // 硬盘持久化地址
                .with(CacheManagerBuilder.persistence(new File("F:/ehcacheData", cacheName)))
                // 设置一个默认缓存配置
                .withCache(cacheName, cacheConfiguration)
                //创建之后立即初始化
                .build(true);

    }

    /**
     * 数据缓存
     * @param cacheName
     * @param key
     * @param value
     */
    public static void set(String cacheName,String key,String value){

         if(cacheManager==null){
             /*初始化cache*/
             cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
             cacheManager.init();
         }else{
            if(cacheManager.getStatus()==Status.UNINITIALIZED){
                cacheManager.init();
            }
         }
       //
        if(cacheManager.getCache(cacheName, String.class, String.class)==null){
            System.out.println(">><<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>aa");
            //初始化
            initEhcache(cacheName,key,value,0);
        }
        Cache<String, String> mineCache = cacheManager.getCache(cacheName, String.class, String.class);
        mineCache.put(key,value);
        String rs = mineCache.get(key);
        System.out.println("set>>>>>>"+rs);
    }

    /**
     * 数据缓存 支持过期时间
     * @param cache
     * @param key
     * @param value
     * @param expiry
     */
    public static void set(String cache,String key,String value,long expiry){
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
        /*初始化cache*/
        cacheManager.init();
        if(cacheManager.getCache(cache, String.class, String.class)==null){
            //初始化
           // EhcacheUtils.initEhcache(cache,expiry);
        }
        Cache<String, String> mineCache = cacheManager.getCache(cache, String.class, String.class);
        mineCache.put("key", "valuessssssssssssssssssssss");
        cacheManager.close();

    }

    /**
     * 根据key获取数据
     * @param cache
     * @param key
     * @return
     */
    public static String get(String cache,String key){
        if(cacheManager==null){
            //初始化cache
            cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
            cacheManager.init();
        }
        Cache<String, String> mineCache = cacheManager.getCache(cache, String.class, String.class);
        String rs = mineCache.get(key);
        cacheManager.close();
        return  rs;
    }

}
